package com.seven.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seven.bus.entity.BusLine;
import com.seven.bus.mapper.BusLineMapper;
import com.seven.bus.service.BusLineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author seven
 * @since 2021-01-12
 */
@Service
public class BusLineServiceImpl extends ServiceImpl<BusLineMapper, BusLine> implements BusLineService {

    @Override
    public String getLine(Integer startId, Integer endId) {
        List<BusLine> list = list(new QueryWrapper<BusLine>().lambda().in(BusLine::getBusStationId, Arrays.asList(startId, endId)));
        Map<Integer, List<BusLine>> collect = list.stream().collect(Collectors.groupingBy(BusLine::getBusInfoId));
        collect.values().forEach(s -> {
            if (s.size() > 1) {
                System.out.println("直达线路：" + s);
            }
        });
        //2.
        List<BusLine> startList = list(new QueryWrapper<BusLine>().lambda().eq(BusLine::getBusStationId, startId));
        System.out.println("起点" + startList);
        if (!startList.isEmpty()) {
            startList.forEach(s -> {
                List<BusLine> lines = list(new QueryWrapper<BusLine>().lambda().eq(BusLine::getBusInfoId, s.getBusInfoId()));
                lines.forEach(sb -> {
                    List<BusLine> list1 = list(new QueryWrapper<BusLine>().lambda()
                            .ne(BusLine::getBusInfoId, sb.getBusInfoId())
                            .in(BusLine::getBusStationId, Arrays.asList(sb.getBusStationId(), endId)));
                    list1.stream().collect(Collectors.groupingBy(BusLine::getBusInfoId)).values().forEach(t -> {
                        if (t.size() > 1) {
                            System.out.println("换乘：" + sb);
                            System.out.println("路线：" + t);
                        }
                    });
                });
            });
        } else {
            System.out.println("无效起点~");
        }
        return null;
    }
}
